<?php


class IApplication implements ISingleton {
	private static $instance;
	private static $started = false;
	private $root;
	private $defaultRoot = "./ipa";
	private $locale;
	private static $modules = array();
	
	/**
	 * Enter description here...
	 *
	 * @return IApplication
	 */
	public static function singleton() {
		if (self::$instance == null) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function version() {
		return "I-F_v1.0";
	}
	
	public function setRoot($root) {
		$this->root = $root;
	}
	
	public function root() {
		$root = null;
		if ($this->root != null) {
			$root = $this->root;
		}
		else if (defined("IF_APP_ROOT")) {
			$root = IF_APP_ROOT;
		}
		else {
			$root = $this->defaultRoot;
		}
		if (is_dir($root)) {
			$this->root = realpath($root);
		}
		else {
			$this->root = $root;
		}
		return $this->root;
	}
	
	public static function initialize() {
		//检查PHP版本
		if (version_compare(PHP_VERSION, "5.1.3") < 0) {
			exit("I-Framework must runs on PHP >= 5.1.3 (current PHP version on your server is " . PHP_VERSION . ")");
		}
		
		$app = self::singleton();
		$vm = IVM::singleton();
		
		//运行配置
		//include_path
		$includePaths = IConfig::opt("@app.include_path");
		if (is_array($includePaths)) {
			foreach ($includePaths as $key=>$path) {
				if (!$vm->isAbsolutePath($path)) {
					$path = $app->root() . "/" . $path;
				}
				if (is_dir($path)) {
					$includePaths[$key] = realpath($path);
				}
			}
			set_include_path(rtrim(get_include_path(), PATH_SEPARATOR) . PATH_SEPARATOR . implode(PATH_SEPARATOR, $includePaths));
		}
		
		//class_path
		$classPaths = IConfig::opt("@app.class_path");
		foreach ($classPaths as $key=>$path) {
			if (!$vm->isAbsolutePath($path)) {
				$path = $app->root() . "/" . $path;
			}
			if (is_dir($path)) {
				$vm->addClassPath(realpath($path));
			}
		}
		
		//class_indexes
		$modules = IConfig::opt("@app.modules");
		$_3rdModules = IConfig::opt("@app.3rd_modules");
		self::$modules = array_merge(self::$modules, $modules);
		self::$modules = array_merge(self::$modules, $_3rdModules);
		foreach ($modules as $dir) {
			$configFile = IF_FW_DIR . "/" . $dir . "/_class.php";
			if (is_file($configFile)) {
				$classes = $vm->loadFile($configFile);
				if (is_array($classes)) {
					addPrefix($classes, IF_FW_DIR . "/" . $dir . "/");
					$vm->addClassIndexes($classes);
				}
			}
		}
		foreach ($_3rdModules as $dir) {
			$configFile = $this->relativeFile($dir . "/_class.php");
			if ($configFile->isFile()) {
				$classes = $vm->loadFile($configFile->absPath());
				if (is_array($classes)) {
					addPrefix($classes, IF_APP_ROOT . "/" . $dir . "/");
					$vm->addClassIndexes($classes);
				}
			}
		}
	}
	
	public static function start($router = true) {
		//设置已启动
		self::$started = true;
		self::initialize();
		
		if ($router) {
			$router = new IRouter();
			$router->start();
		}
	}
	
	public static function started() {
		return self::$started;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $path
	 * @return IFile
	 */
	public function relativeFile($path) {
		if (IVM::singleton()->isAbsolutePath($path)) {
			return new IFile($path);
		}
		return new IFile($this->root() . "/" . $path);
	}
	
	/**
	 * Enter description here...
	 *
	 * @return ILocale
	 */
	public function locale() {
		if ($this->locale == null) {
			$this->locale = ILocale::auto();
		}
		return $this->locale;
	}
	
	public static function hasModule($module) {
		return isset(self::$modules[$module]);
	}
}

?>